/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vn.webapp.controller.cp;

import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import vn.webapp.controller.BaseWeb;
import vn.webapp.dto.DataPage;
import vn.webapp.form.CourseValidationForm;
import vn.webapp.model.Courses;
import vn.webapp.service.CoursesService;
/**
 *
 * @author Tonytran
 */
@Controller("cpCourses")
@RequestMapping(value = {"/cp"})
public class CoursesController extends BaseWeb{
    
    @Autowired
    private CoursesService coursesService;
    
    private int nav = 3;
    /**
     * 
     * @param model
     * @return 
     */
    @RequestMapping(value = "/courses" , method = RequestMethod.GET)
    public String courses(ModelMap model, HttpSession session,
    @RequestParam(value = "page", defaultValue = "0") int page){
        //DataPage<Courses> dataCourses = coursesService.list();
        int pageSize = 10;        
        DataPage<Courses> dataCourses = coursesService.filter(page, pageSize);
        model.put("sz_Response", session.getAttribute("response"));
        session.setAttribute("response", "");
        model.put("dataCourses", dataCourses);
        model.put("nav", this.nav);
        model.put("pageIndex", page);
        return "cp.courses";
    }
    
    /**
     *
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("/coursedetail/{id}")
    public String viewDetail(ModelMap model, @PathVariable("id") String id) {
        Courses course = coursesService.viewDetail(id);
        model.put("courseForm", new CourseValidationForm());
        model.put("dataCourse", course);
        model.put("nav", this.nav);
        return "cp.coursedetail";
    }
    

    @RequestMapping(value = "/savecoursedetail", method = RequestMethod.POST)
    public String processEditClass(@Valid @ModelAttribute("courseForm") CourseValidationForm courseForm,
            BindingResult result,HttpServletRequest request, Map model, HttpSession session) {
        
        // Add the saved validationForm to the model  
        model.put("nav", this.nav);
        if (result.hasErrors()) {
           return "cp.coursedetail";
        }else
        {
            String CourseCode = courseForm.getCode();
            String CourseName = courseForm.getName();
            int Credits = courseForm.getCredits();
            int CourseId = courseForm.getId();
            Courses o_Course = coursesService.checkExistsCode(CourseCode, CourseId);
            if(o_Course != null)
            {
                model.put("sz_Response", "The code "+CourseCode+" is being used.");
                return "cp.coursedetail";
            }else{
                int i_UpdateCourse = coursesService.updateTheCourse(CourseCode, CourseName, CourseId, Credits);
                session.setAttribute("response", "Edit successfully!");
                return "redirect:" + this.baseUrl + "/cp/courses.html";
            }
        }
    }
    
    @RequestMapping(value = "/removecourse/{id}", method = RequestMethod.GET)
    public String processRemoveCourse(ModelMap model, @PathVariable("id") int id, HttpSession session) {
        int i_IsRemoved = coursesService.removeCourse(id);
        String sz_Response;
        if (i_IsRemoved != 0) {
            sz_Response = "Remove the course successfully !";
        } else {
            sz_Response = "Remove the course not successfully. Please retry";
        }
        session.setAttribute("response", sz_Response);
        return "redirect:" + this.baseUrl + "/cp/courses.html";
    }
    
    @RequestMapping(value = "/addnewcourse", method = RequestMethod.GET)
    public String addnewcourse(Map model){
        model.put("nav", this.nav);
        model.put("courseFormAdd", new CourseValidationForm());
        return "cp.addnewcourse";
    }
    
    @RequestMapping(value = "/savecourse", method = RequestMethod.POST)
    public String processAddNewCourse(@Valid @ModelAttribute("courseFormAdd") CourseValidationForm courseFormAdd,
            BindingResult result, Map model, HttpSession session) {
        
        // Add the saved validationForm to the model  
        model.put("nav", this.nav);
        if (result.hasErrors()) {
           return "cp.addnewcourse";
        }else
        {
            String CourseCode = courseFormAdd.getCode();
            String CourseName = courseFormAdd.getName();
            int Credits = courseFormAdd.getCredits();
            int CourseId = courseFormAdd.getId();
            Courses o_Course = coursesService.checkExistsCode(CourseCode, CourseId);
            if(o_Course != null)
            {
                model.put("sz_Response", "The code "+CourseCode+" is being used.");
                return "cp.addnewcourse";
            }else
            {
                int i_InsertACourse = coursesService.saveACourse(CourseCode, CourseName, Credits);
                session.setAttribute("response", "Add successfully!");
                return "redirect:" + this.baseUrl + "/cp/courses.html";
            }
        }
    }
}
